package com.patrrr.SQLite3;

import com.patrrr.app.NoteInfo;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DBHelper extends SQLiteOpenHelper {
	private static final int DEFAULT_VERSION = 1;
	private static final String DB_NAME = "Notes";

	public static final String ID = "_id";

	/**
	 * 便签表，所有便签数据保存于此
	 */
	public static final String NOTE = "note"; // 表名
	// 列名
	public static final String NOTE_FOLDERID = "folderId"; // floderId
	public static final String NOTE_CONTENT = "content"; // 内容
	public static final String NOTE_CREATETIME = "createTime"; // 创建时间
	public static final String NOTE_ALERTTIME = "alertTime"; // 提醒时间
	public static final String NOTE_COLOR = "color"; // 颜色
	public static final String NOTE_WIDGETID = "widgetId"; // WidgetId

	// 目录表，所有目录数据保存于此
	public static final String FOLDER = "note_folder";
	public static final String FOLDER_NAME = "name";

	/**
	 * 创建便签表语句 _id(integer)|folderId(integer)|content(varchar)|createTime(long)|
	 * alertTime(long)|color(integer)|widgetId(integer)
	 */
	public static final String CREATE_NOTETABLE_SQL = "create table " + NOTE
			+ "(" + ID + " integer primary key autoincrement," + NOTE_FOLDERID
			+ " integer," + NOTE_CONTENT + " varchar," + NOTE_CREATETIME
			+ " long," + NOTE_ALERTTIME + " long," + NOTE_COLOR + " integer,"
			+ NOTE_WIDGETID + " integer)";
	/**
	 * // _id(integer)|name(varchar)
	 */
	public static final String CREATE_FOLDER_SQL="create table " + FOLDER + "(" + ID
	+ " integer primary key autoincrement," + FOLDER_NAME
	+ " varchar)";

	public DBHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	public DBHelper(Context context, String name, int version) {
		super(context, name, null, version);
	}

	public DBHelper(Context context, String name) {
		super(context, name, null, DEFAULT_VERSION);
	}

	public DBHelper(Context context) {
		super(context, DB_NAME, null, DEFAULT_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		// 创建便签表
		db.execSQL(CREATE_NOTETABLE_SQL);
		// 创建目录表
		db.execSQL(CREATE_FOLDER_SQL);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

	/**
	 * 获取格式化的sqlite3创建表语句
	 * _id(integer)|folderId(integer)|content(varchar)|createTime
	 * (long)|alertTime (long)|color(integer)
	 * 
	 * @param tableName
	 * @return
	 */
	public static String getSQL(String tableName) {
		return "create table " + tableName + "(" + ID
				+ " integer primary key autoincrement," + NOTE_FOLDERID
				+ "integer," + NOTE_CONTENT + " varchar," + NOTE_CREATETIME
				+ " long," + NOTE_ALERTTIME + " long," + NOTE_COLOR
				+ " integer,"+NOTE_WIDGETID+" integer)";
	}

}
